package Test28;

import java.util.Objects;

//Objects工具类的使用
/*
public static boolean equals(object a, object b)        先做非空判断，比较两个对象
public static boolean isNull(object obj)                判断对象是否为null，为null返回true ,反之
public static boolean nonNull(object obj)               判断对象是否为null，跟isNull的结果相反

 */
public class Test11 {
    public static void main(String[] args) {

        //1、创建学生类的对象
        Student11 s1 = null;
        Student11 s2 = new Student11("zhangsan", 23);

        //2、比较两个对象的属性值是否相同
        /*if (s1 != null) {
            boolean result = s1.equals(s2);
            System.out.println(result);
        } else {
            System.out.println("调用者为空");
        }*/
        boolean result = Objects.equals(s1, s2);
        System.out.println(result);
        //细节:
        //1.方法的底层会判断s1是否为nul1，如果为null，直接返回false
        //2.如果s1不为nul1，那么就利用s1再次调用equals方法
        //3.此时s1是student类型，所以最终还是会调用student中的equals方法。
        //  如果没有重写，比较地址值，如果重写了，就比较属性值。

        Student11 s3 = new Student11();
        Student11 s4 = null;
        System.out.println(Objects.isNull(s3));//false
        System.out.println(Objects.isNull(s4));//true

        System.out.println(Objects.nonNull(s3));//true
        System.out.println(Objects.nonNull(s4));//false

    }
}

class Student11 {
    private String name;
    private int age;

    public Student11() {
    }

    public Student11(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student11 student11 = (Student11) o;
        return Objects.equals(name, student11.name) && Objects.equals(age, student11.age);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Student11{" + "name='" + name + '\'' + ", age='" + age + '\'' + '}';
    }
}
